﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// 逻辑单元处理方法的代码生成器
/// </summary>
public class LogicUnitMethodCrtete:UnitMethodCreate
{
    /// <summary>
    /// 生成数据
    /// </summary>
    /// <param name="txtAfn">AFN值 十六进制</param>
    /// <param name="txtFn">FN值,多个,分开</param>
    /// <param name="EntityTemplate">数据实体单元模板</param>
    /// <param name="EntityMethodTemplate">数据实体模板方法</param>
    public override void BulidCode(string txtAfn, string txtFn, string EntityTemplate, string EntityMethodTemplate)
    {
        if (string.IsNullOrEmpty(txtAfn) || string.IsNullOrEmpty(txtFn))
        {
            return;
        }
        //1.检查文件夹是否存在
        //2.创建文件夹
        //3.跟据AFN创建 文件
        //3.1 数据单元,
        //3.2 数据单元操作方法

        string codePath = HttpContext.Current.Server.MapPath("./LogicCode/");
        //查询
        var afnque = summaryList.Where(p => p.AFNHEX == txtAfn.Trim().ToUpper());
        if (afnque.Count() > 0)
        {
            AFNSummary afnEntity = afnque.FirstOrDefault();
            string dir = codePath + "AFN" + afnEntity.AFNHEX;
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }
            //开始创建FN单元与FN的操作方法
            foreach (string fn in txtFn.Split(','))
            {
                string filenameUnit = "#AFNNAME#Logic#FN#";
                string filenameMethod = " #AFNNAME#Logic#FN#Method";

                #region 替换数据
                filenameUnit = filenameUnit
                                       .Replace("&lt;", "<")
                          .Replace("&gt;", ">")
                    .Replace(afnName, afnEntity.AFNNAME)
                         .Replace(FN, "F" + fn)
                         .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                         .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                         .Replace("afnSummary", afnEntity.Summary);
                filenameMethod = filenameMethod
                                       .Replace("&lt;", "<")
                                   .Replace("&gt;", ">")
                                .Replace(afnName, afnEntity.AFNNAME)
                                  .Replace(FN, "F" + fn)
                                  .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                                  .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                                  .Replace("afnSummary", afnEntity.Summary);
                #endregion


                string contentUnit = EntityTemplate
                                       .Replace("&lt;", "<")
                     .Replace("&gt;", ">")
                    .Replace(afnName, afnEntity.AFNNAME)
                    .Replace(FN, "F" + fn)
                    .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                    .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                    .Replace(afnSummary, afnEntity.Summary);

                string contentMethod = EntityMethodTemplate
                   .Replace("&lt;", "<")
                   .Replace("&gt;", ">")
                  .Replace(afnName, afnEntity.AFNNAME)
                  .Replace(FN, "F" + fn)
                  .Replace(Afnstr, " (byte)InCom.Utilities.Protocol.AFN." + afnEntity.AFNValue)  // (byte)InCom.Utilities.Protocol.AFN.RESPOND
                  .Replace(date, DateTime.Now.ToString("yyyy-MM-dd"))
                  .Replace(afnSummary, afnEntity.Summary);


                //写文件,不进行数据实体写入
                //System.IO.File.WriteAllText(dir + "/" + filenameUnit + ".cs", contentUnit);
                System.IO.File.WriteAllText(dir + "/" + filenameMethod + ".cs", contentMethod);


            }
        }
        else
        {
            HttpContext.Current.Response.Write("指定的AFN不存在");
        }
    }
}